define(['app'], function( app ) {
    'use strict';
        app.directive('jspagination', function ( $timeout ) {
        return{
            restrict: 'EA',
            replace:true,
            transclude : true,
            scope:{
                page : '=pageObject',
                query : '=pageFunc'
            },
            template :'<div>'
                +'<span style="float:right;margin-top:12px;margin-left:12px;">共 {{page.pageAll}} 页</span>'
                +'<div  id={{page.pageName}} class="ui pagination menu"></div>'
                +'<span style="float:right;margin-top:12px;margin-right:12px;">共 {{page.dataCount}} 条记录</span>'
                +'</div>',
            link : function ( $scope , $element) {
                function page_item(p, p_now, p_all, p_show) {
                    if(p>p_all) return '';
                    var str = '';
                    str = '<a  class="item '+ ((p==p_now) ?' active' : ' ') + '" nextpage="'+p+'">'+p_show+'</a>';
                    if (p_all>1) {
                        if(p==1){
                            str = '<a nextpage="-1" class="item'+((p_now==1) ? ' disabled' : '')+'" ><i class="caret left icon"></i> </a>'  + str;
                        }
                        else if(p==p_all){
                            str += '<a nextpage="+1"  class="item'+((p_now==p_all) ? ' disabled' : '')+'" ><i class="caret right icon"></i> </a>';
                        }
                    }
                    return str;
                }
                $scope.createHtml = function () {
                    $scope.uniqueId = $scope.$id;//wantch参数
                    var maxPage =  $scope.page.pageAll ;//分页总数
                    var pageNo =  $scope.page.pageNow;//当前页,从 1 开始
                    var str = '';

                    var p_step = 2,p_more = p_step*2+1;//当前页面前后多于 3 条数据开始，显示更新
                    var middle = 0;
                    middle = maxPage ? Math.ceil(maxPage/2):0;
                    if(maxPage <= p_more){
                        for (var i = 1; i <= maxPage; i++) {
                            str += page_item(i,pageNo,maxPage,i);
                        }
                    }
                    else{
                        var middle = 0;
                        var _p = {};
                        _p['first'] = ((pageNo - p_step)>=2) && ((pageNo+p_step)<maxPage) ? true:false;
                        _p['now'] = 0;
                        str = page_item(1, pageNo, maxPage, 1); 

                        if (_p['first'] ){//客户展开页数
                            _p['now'] = pageNo - p_step;
                            if ( _p['now']>2){
                                middle = Math.ceil( (pageNo - p_step)/2);
                                str += page_item(middle, pageNo, maxPage,'...');
                            }
                            for (;_p['now']<=(pageNo+p_step)&&_p['now']<maxPage;_p['now']++) {
                                str += page_item(_p['now'], pageNo, maxPage,_p['now']);
                            }
                        }
                        else{//当前分页不够分
                            _p['now'] = 2;
                            for (var i = 2;(i+p_step)<=p_more; i++) {
                                _p['now']+=1;
                                str += page_item(i, pageNo, maxPage,i);
                            }
                        }
                        
                        middle = Math.ceil( (maxPage - _p['now'])/2) + _p['now'];
                        if ((maxPage - _p['now'] )>=p_step ){
                            str += page_item(middle, pageNo, maxPage,'...');
                        }
                        _p['last'] =  (maxPage - _p['now']) <= p_step ? true:false;
                        if(_p['last'] || pageNo<(p_more-1) ){
                            if (pageNo<(p_more-1) ){
                                _p['now'] = maxPage - p_step ;
                                for (_p['now']; _p['now']<maxPage&&_p['now']>0; _p['now']++) {
                                    str += page_item(_p['now'], pageNo, maxPage, _p['now']);
                                }
                            }
                            else if ((maxPage - _p['now'])<=p_step){
                                for (;_p['now']<maxPage&&_p['now']>0; _p['now']++) {
                                    str += page_item(_p['now'], pageNo, maxPage, _p['now']);
                                }
                            }
                            else{
                                    if ((maxPage - _p['now'])>2 )
                                         _p['now'] = maxPage;
                                    for (;_p['now']<maxPage&&_p['now']>0; _p['now']++) {
                                        str += page_item(_p['now'], pageNo, maxPage, _p['now']);
                                    }
                                }
                        }                    
                        str += page_item(maxPage, pageNo, maxPage, maxPage);
                    }
                    var pageName = $scope.page.pageName;
                    if(pageName){
                        $("#"+pageName).parent().removeClass('menu');//增加显示数据，移除menu（去掉外层框）
                        $("#"+pageName).html(str);
                    }else{
                        $element.html(str);
                    }

                    $scope.bindEvent();
                };
                $scope.bindEvent = function () {
                    $element.find('a').on('click', function ()
                    {
                        if( !$(this).hasClass("disabled") && !$(this).hasClass("active") )
                        {
                            var text = $(this).attr("nextpage");
                            var pageNo = $scope.page.pageNow;//上一次的页数
                            if($.trim(text) == '-1')
                            {
                                if( pageNo > 1)
                                    $scope.page.pageNow = pageNo - 1 ;
                            }else if($.trim(text) == '+1'){
                                if( $scope.page.pageAll > pageNo )
                                $scope.page.pageNow = pageNo + 1 ;
                            }else{
                                 $scope.page.pageNow = parseInt(text);
                            }
                            $scope.query();
                            $scope.createHtml();
                        }
                    });
                }
                $scope.createHtml();
                $scope.$watch('page.pageAll + 1000*page.pageNow + uniqueId*100', function () {
                    $scope.createHtml();
                })
            }
        }
    });
});